<?php
require_once (ROOTDIR."common/db_utils.php");

function addproblem($title, $time_limit, $memory_limit, $description,
					$input, $output, $sample_input, $sample_output,
                    $hint, $source, $level, $spj, $datadir, $fromoj=null) {

	$sql = "INSERT into `problem` (`title`,`time_limit`,`memory_limit`,	`description`,
				`input`,`output`,`sample_input`,`sample_output`,`hint`,`source`, `level`, 
				`spj`,`in_date`,`defunct`, `creater_id`, `fromoj`) 
				VALUES(?, ?, ?, ?, ?,?,?,?,?,?,?,?, NOW(),'Y', ?, ?)";

	$pid = DbUtil::getInstance()->query( $sql,
        $title, $time_limit, $memory_limit, $description,
		$input, $output, $sample_input, $sample_output,
        $hint, $source, $level, $spj, $_SESSION['user_id'], $fromoj);

	echo "<br>添加题目成功，题目编号: $pid <br>";
	/*
	if (isset ( $_POST ['contest_id'] )) {
		$cid =intval($_POST ['contest_id']);
		$sql = "select count(*) FROM `contest_problem` WHERE `contest_id`=?";
		$result = DbUtil::getInstance()->query( $sql, $cid ) ;
		$row =$result[0];
		$num = $row [0];
		echo "Num=" . $num . ":";
		$sql = "INSERT INTO `contest_problem` (`problem_id`,`contest_id`,`num`) VALUES(?,?,?)";
        DbUtil::getInstance()->query($sql,$pid,$cid,$num);
	}
	*/
	$basedir = "$datadir/$pid";
	echo "[$title]data in $basedir <br>";
	return $pid;
}

function mkdata($pid, $filename, $input, $datadir){
	$basedir = "$datadir/$pid";
	
	$fp = fopen ( $basedir."/$filename", "w" );
	if( $fp ) {
		fputs ( $fp, preg_replace ( "(\r\n)", "\n", $input ) );
		fclose ( $fp );
	}else{
		echo "创建文件失败".$basedir
			."/$filename ,try [chgrp -R www-data $datadir] and [chmod -R 771 $datadir ]";
	}
}

?>